<template>
  <div mode="selector" @click="jumpToPartment">
    <div class="form-item">
      <div class="label">
        {{ label }}
      </div>
      <div class="content">
        <div v-if="nameString == ''" class="placeholder">
          {{ placeholder }}
        </div>
        <div v-else style="max-width: 200px;line-height: 18px;">
          {{ nameString }}
        </div>
        <arrow-right />
      </div>
    </div>
  </div>
</template>

<script>
import ArrowRight from '@/components/ze-oa-form/cpns/arrow-right.vue'

export default {
  name: 'ZePicker',
  components: { ArrowRight },
  props: {
    label: { type: String, default: '' },
    placeholder: { type: String, default: '请选择' },
    options: { type: Array, default: () => [] },
    labelKey: { type: String, default: 'label' },
    valueKey: { type: String, default: 'value' },
    value: {},
  },
  data() {
    return {
      selectedItem: null,
      nameString: '',
      partData: {},
    }
  },
  mounted() {
    this.getData()
    uni.$on('edit2', (editdata) => {
      // console.log('点击编辑设置参与者')
      // this.nameString = ''
      this.$nextTick(function() {
        this.getData()
      })
    })
    uni.$on('partPageChange', (data) => {
      console.log(data, '参与者选中数据')
      this.nameString = ''
      for (let i = 0; i < data.partData.length; i++) {
        if (i == data.partData.length - 1) {
          this.nameString = `${this.nameString}${data.partData[i].username}`
        } else {
          this.nameString = `${this.nameString}${data.partData[i].username},`
        }
      }
      this.partData = data.partData
      // this.deptId = data.deptId
      // this.deptName = data.deptName
    })
  },
  methods: {
    getData() {
      // console.log('参与者又执行了一遍')
      this.nameString = ''
      try {
        const value = uni.getStorageSync('storage_part')
        // console.log(value, 'value')
        if (value) {
          // console.log(value, '参与人')
          if (value.partData.length > 0) {
            for (let i = 0; i < value.partData.length; i++) {
              if (i == value.partData.length - 1) {
                this.nameString = `${this.nameString}${value.partData[i].username}`
              } else {
                this.nameString = `${this.nameString}${value.partData[i].username},`
              }
            }
          }
          this.partData = value.partData
          // console.log(this.nameString, 'this.nameString')
        }
      } catch (e) {
        // error
      }
    },
    // 点击跳转
    jumpToPartment() {
      // uni.navigateTo({
      //   url: '/pages/office/project-approval/approval-form/part-list',
      // })redirectTo
      uni.navigateTo({
        // url: `/pages/office/project-approval/approval-form/part-list?techId=${this.techId}&techName=${this.techName}`,
        url: `/pages/office/project-approval/approval-form/part-list?partData=${JSON.stringify(
          this.partData,
        )}`,
      })
    },
    handleChange(e) {
      this.selectedItem = this.options[e.detail.value]
      this.$emit('input', this.selectedItem[this.valueKey])
    },
  },
}
</script>

<style lang="scss" scoped>
.form-item {
  font-size: 14px;
  font-weight: 400;
  line-height: 14px;
  padding: 15px 16px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #f2f2f2;
  .label {
    color: #333333;
    // font-size: 14px;
  }

  .content {
    display: flex;
    align-items: center;
    column-gap: 5px;
  }

  .placeholder {
    color: #999999;
  }
}
</style>
